﻿/*
    Copyright (c) 2011, Jeremy Randall (http://www.auburnrandall.com)
      
    All rights reserved.
 
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
 
    1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
 
    2. Redistributions in binary form must reproduce the above copyright 
    notice, this list of conditions and the following disclaimer in 
    the documentation and/or other materials provided with the distribution.
 
    3. The names of the authors may not be used to endorse or promote products
    derived from this software without specific prior written permission.
 
    THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
    OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Xml;
using System.Xml.Serialization;

namespace FormEngine
{
    /// <summary>
    /// Attributes describing a form.
    /// </summary>
    [Serializable()]
    public class FormProperties
    {
        public FormProperties()
        {
            Location = string.Empty;
            OwnerName = string.Empty;
            Title = string.Empty;
            Instructions = string.Empty;
        }

        /// <summary>
        /// Path to the form.
        /// </summary>
        [XmlAttribute("location")]
        public string Location { get; set; }

        /// <summary>
        /// Name for the form owner. Matches the Identity.Name property.
        /// </summary>
        [XmlAttribute("ownerName")]
        public string OwnerName { get; set; }

        /// <summary>
        /// Title of the form.
        /// </summary>
        [XmlAttribute("title")]
        public string Title { get; set; }

        /// <summary>
        /// Date that the form will expire. This can happen for nonpayment.
        /// </summary>
        [XmlAttribute("expirationDate")]
        public DateTime ExpirationDate { get; set; }

        public string Instructions { get; set; }

        /// <summary>
        /// Unique identifier for the form.
        /// </summary>
        public string FormId { get; private set; }

        /// <summary>
        /// Loads a FormProperties instance based on previously serialized data.
        /// </summary>
        /// <param name="formId">Form ID for the form that will be loaded.</param>
        /// <returns>FormProperties instance</returns>
        public static FormProperties Load(string formId)
        {
            FormProperties fp = FormDataProvider.Instance.LoadFormProperties(formId);
            fp.FormId = formId;

            return fp;
        }

        /// <summary>
        /// Saves the FormProperties instance to an XML file in the provided path.
        /// </summary>
        public void Save()
        {
            // New form
            if (FormId == null)
            {
                FormId = Guid.NewGuid().ToString("N");
            }

            FormUser user = FormUser.Load(OwnerName);
            user.AddForm(this);

            FormDataProvider.Instance.SaveFormProperties(this);
        }

        /// <summary>
        /// Removes the form properties definition
        /// </summary>
        internal void Delete()
        {
            FormDataProvider.Instance.DeleteFormProperties(this);
        }
    }
}
